package toubiao.controller;


import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import toubiao.exception.CustomException;
import toubiao.service.DownloadServiceI;
import toubiao.service.impl.ProjectServiceImpl;
import toubiao.utils.ProUtilFileSuffix;
import toubiao.utils.filter.FilterCondition;
import toubiao.utils.filter.FilterHqlFactory;
import toubiao.utils.filter.OrderCondition;
import toubiao.utils.filter.OrderHqlFactory;
import utils.UtilController;
import utils.UtilDate;
import utils.UtilString;

@Controller
@RequestMapping("/download")
public class DownloadController {
	static private  Logger logger=LoggerFactory.getLogger(DownloadController.class);
	
	@Autowired
	private ProjectServiceImpl projectService;
	
	
	@Autowired
	private DownloadServiceI downloadService;

	@RequestMapping("/public")
	public void downloadPublicFile(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, CustomException{
		String fileName=request.getParameter("fileName");
		String errorMsg;
		logger.info(fileName);
		if(UtilString.isNullOrEmpty(fileName)){
			errorMsg="下载文件名为空";
			request.setAttribute("errorMsg", errorMsg);
			request.getRequestDispatcher("/WEB-INF/jsp/error/errorMsg.jsp").forward(request, response);
		}
		logger.info("fileName = "+fileName );
		String fileAbsPath=downloadService.getPublicFileAbsPath(fileName);
		UtilController.downloadFile(fileAbsPath, fileName, request, response);
	}
	
	@RequestMapping("/grid/all/{objectName}/{fileType}")
	public void fileDownloadGridAll(@PathVariable("objectName") String objectName, @PathVariable("fileType") String fileType,HttpServletRequest request, HttpServletResponse response) throws CustomException, IOException{
		String fileAbsPath="";
		if(objectName.trim().equalsIgnoreCase("project")){
			fileAbsPath=projectService.downloadFileAll(fileType);
		}else if(objectName.trim().equalsIgnoreCase("contract")) {
			
		}
		
		String suffix=ProUtilFileSuffix.getFileSuffix(fileType);
        String dateString=UtilDate.getFormateDate(new Date(), "yyMMdd");
        String savedName="所有项目_"+dateString+suffix;
        UtilController.downloadFile(fileAbsPath, savedName, request, response);
	}

	//下载Excel checked
	@RequestMapping("/downloadChecked")
	public void fileDownloadGridChecked(HttpServletRequest request, HttpServletResponse response,String ids) throws Exception{
		if(ids==null || ids.trim().equals("")){
			throw new CustomException("ids 为空.");
		}
		String fileType=request.getParameter("fileType");
		String suffix=ProUtilFileSuffix.getFileSuffix(fileType);
		String fileAbsPath=projectService.downloadFileCheckedByIds(fileType, ids);
		
        String dateString=UtilDate.getFormateDate(new Date(), "yyMMdd");
        String savedName="勾选项目_"+dateString+suffix;
        UtilController.downloadFile(fileAbsPath, savedName, request, response);
	}
	
	//下载Excel filtered
	@RequestMapping("/downloadFiltered")
	public void fileDownloadGridFiltered(HttpServletRequest request, HttpServletResponse response) throws CustomException, IOException{
		String filterConditionsJson=request.getParameter("conditionsJson");
		String orderConditionsJson=request.getParameter("orderConditionJson");
		String fileType=request.getParameter("fileType");
		String suffix=ProUtilFileSuffix.getFileSuffix(fileType);
		List<FilterCondition> filterConditionList=FilterHqlFactory.json2FilterConditionList(filterConditionsJson);
		List<OrderCondition> orderConditionList=OrderHqlFactory.json2OrderConditionList(orderConditionsJson);
		
		String fileAbsPath=projectService.downloadFileByConditions(fileType, filterConditionList, orderConditionList);
		String dateString=UtilDate.getFormateDate(new Date(), "yyMMdd");
        String savedName="筛选项目_"+dateString+suffix;
        UtilController.downloadFile(fileAbsPath, savedName, request, response);
	}
}
